//
//  UIImageView+WebImage.swift
//  斯威夫特微博
//
//  Created by youyoumacmini3 on 2017/7/20.
//  Copyright © 2017年 youyoumacmini3. All rights reserved.
//

import SDWebImage

extension UIImageView {
    
    
    /// 隔离 SDWebImage 设置图像函数
    ///
    /// - Parameters:
    ///   - urlStr: urlStr
    ///   - placeHolderImage: 占位图
    ///   - isAvatar: 是否头像
    func cz_setImage(urlStr: String?, placeHolderImage:UIImage?, isAvatar: Bool = false) {
        
        // 处理 url
        guard let urlStr = urlStr,
              let url = URL(string: urlStr) else {
            // 设置占位图
            image = placeHolderImage
            return
        }
        
        sd_setImage(with: url, placeholderImage: placeHolderImage, options: [], progress: nil) { [weak self] (image, _, _, _) in
            // 上面加个 [weak self] 也防止出现循环引用问题
            // 完成回调 - 判断是否是头像
            if isAvatar {
                self?.image = image?.cz_avatarImage(size: self?.bounds.size, backColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), lineColor: #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1))
            }
            
            
        }
    }
    
}
